// pages/seach-detail/index.js
import {getSearchHot,getCloudsearch,getSearchSuggest} from "../../service/api_search"
import debounce from "../../utils/debounce"
const debounceChange = debounce(getSearchSuggest)
Page({

    /**
     * 页面的初始数据
     */
    data: {
       hots:[],
       suggestSongs:[],
       searchValue:"",
       songs:[]
    },
    //搜索建议
    onChange(e){
       // console.log(e.detail)
       const keyword = e.detail.replace(/\s*/g,"")
       if(!keyword.length) return
       this.setData({searchValue:keyword})
      // console.log(e.detail)
       debounceChange(keyword).then((res)=>{
            
           // console.log(res)
            this.setData({suggestSongs:res.result.allMatch})
        })
    },
    //搜索歌曲
    searchSong(item){
        const keyword = item.currentTarget.dataset.keyword
         //console.log(item.currentTarget)
         this.setData({suggestSongs:[],songs:[]})
         this.setData({searchValue:keyword,suggestSongs:[]})
         getCloudsearch(keyword).then((res)=>{
         //    console.log(res.result.songs)
             this.setData({songs:res.result.songs})
         })
    },
    //enter按键搜索
    onSearch(e){
    
      const keyword = e.detail.replace(/\s*/g,"")
     // console.log(keyword)
      if(!keyword.length) return
      this.setData({suggestSongs:[],songs:[],searchValue:keyword,suggestSongs:[]})
      getCloudsearch(e.detail).then((res)=>{
          console.log(res.result.songs)
          this.setData({songs:res.result.songs})
      })
    },
    //热门搜索
    handleClick(e){
    const first = e.currentTarget.dataset.first
      this.setData({searchValue:first,suggestSongs:[],songs:[],})
      getCloudsearch(first).then((res)=>{
          //console.log(res.result.songs)
          this.setData({songs:res.result.songs})
      })
    },
    onClear(){
       //console.log(123)
       this.setData({
        suggestSongs:[],
        songs:[]
       })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        getSearchHot().then((res)=>{
          //  console.log(res)
            this.setData({hots:res.result.hots})
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
  
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})